package com.ruoyi.web.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eclink.asset.asset.domain.dao.AssetItem;
import com.eclink.asset.enums.AssetItemStateEnum;
import com.eclink.asset.mapper.AssetItemMapper;
import com.eclink.asset.domain.dto.DistributeItem;
import com.eclink.asset.mapper.IndexChartMapper;
import com.eclink.asset.portal.domain.dto.StatisticsChartQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexService {

    @Resource
    private AssetItemMapper assetItemMapper;

    @Resource
    private IndexChartMapper indexChartMapper;

    public Integer getAssetCount(Integer state) {
        QueryWrapper<AssetItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0);
        if (state >= 0) {
            queryWrapper.eq("state", state);
        }
        return assetItemMapper.selectCount(queryWrapper);
    }

    public Integer getAssetTotalCount() {
        return getAssetCount(-1);
    }

    public Integer getAssetCountUsing() {
        return getAssetCount(AssetItemStateEnum.USING.getValue());
    }

    public Integer getAssetCountIdle() {
        return getAssetCount(AssetItemStateEnum.DRAFT.getValue());
    }

    public Integer getAssetCountBorrow() {
        return getAssetCount(AssetItemStateEnum.BORROWED.getValue());
    }

    public Integer getAssetCountHandle() {
        return getAssetCount(AssetItemStateEnum.HANDLE.getValue());
    }

    public Map<Integer, Integer> getAssetCountMap() {
        Map<Integer, Integer> map = new HashMap<>();
        Integer[] stateList = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        for(Integer state : stateList) {
            map.put(state, getAssetCount(state));
        }
        return map;
    }

    public BigDecimal getAssetTotalAmount() {
        return assetItemMapper.getAssetTotalAmount();
    }


    private String getRateString(BigDecimal rate) {
        return rate.multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).toString() + " %";

    }

    /**
     * 获取资产闲置率
     * @return 结果
     */
    public String getAssetIdleRate() {

        // 所以资产的总数量

        Integer countAll = this.getAssetTotalCount();
        if (countAll == null || countAll == 0) {
            return getRateString(BigDecimal.ZERO);
        }

        QueryWrapper<AssetItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0);
        // 查询闲置资产的总数量
        queryWrapper.eq("state", 0);
        Integer countIdle = assetItemMapper.selectCount(queryWrapper);
        if (countIdle == null || countIdle == 0) {
            return getRateString(BigDecimal.ZERO);
        }

        BigDecimal rate = BigDecimal.valueOf(countIdle).divide(BigDecimal.valueOf(countAll), 4, RoundingMode.HALF_UP);
        return getRateString(rate);
    }

    public List<DistributeItem> getAssetStateDistribute(StatisticsChartQuery query) {
        return indexChartMapper.getAssetStateDistribute(query);
    }

    public List<DistributeItem> getAssetCategoryDistribute() {
        return indexChartMapper.getAssetCategoryDistribute();
    }

    public List<DistributeItem> getAssetWarehouseDistribute() {
        return indexChartMapper.getAssetWarehouseDistribute();
    }

    public List<DistributeItem> getConsumeUseStatistics() {
        return indexChartMapper.getConsumeUseStatistics();
    }

    public List<DistributeItem> getConsumeInStatistics() {
        return indexChartMapper.getConsumeInStatistics();
    }

    public List<AssetItem> getAssetItemList() {

        return assetItemMapper.selectList(null);
    }

}
